<?php
/**
* Contains functions used by AJAX calls.
*/

/**
* Echos the inline edit form for items on the task list.
* @param id The ID of the task to edit.
*/
function gettodoeditform($id) {
    global $onload;
    izsql();
    if(IZ_CALMODE == 'mysql') {
      if($_SESSION['MYSQLi']) {
        global $dblink;
        $todoqry = $dblink->query(sprintf("SELECT * FROM tasks WHERE id = '%s'",$dblink->real_escape_string($id)));
        if($todoqry->num_rows > 0) {
           while($sqltodo = $todoqry->fetch_object()) {
             $todo = new todo($sqltodo);
           }
        }
      } else {
        $todoqry = mysql_query(sprintf("SELECT * FROM tasks WHERE id = '%s'",mysql_real_escape_string($id)));
        if((is_resource($todoqry)) && (mysql_num_rows($todoqry) > 0)) {
           while($sqltodo = mysql_fetch_object($todoqry)) {
             $todo = new todo($sqltodo);
           }
        }
      }
    } elseif(IZ_CALMODE == 'postgre') {
       $todoqry = pg_query(sprintf("SELECT * FROM \"tasks\" WHERE \"id\" = '%s'",pg_escape_string($id)));
       if((is_resource($todoqry)) && (pg_num_rows($todoqry) > 0)) {
          while($sqltodo = pg_fetch_object($todoqry)) {
            $todo = new todo($sqltodo);
          }
       }
    } elseif(IZ_CALMODE == 'msq') {
       $todoqry = mssql_query(sprintf("SELECT * FROM tasks WHERE id = '%s'",mssql_escape_string($id)));
       if((is_resource($todoqry)) && (mssql_num_rows($todoqry) > 0)) {
          while($sqltodo = mssql_fetch_object($todoqry)) {
            $todo = new todo($sqltodo);
          }
       }
    }
    $numqs++;
    if($todo) {
      echo '<form enctype="multipart/form-data" method="post" action="',$_SESSION['IZ_CALURL'],'index.php" id="',$id,'todo" name="',$id,'todo" onsubmit="edittodo(\'',$id,'\'); return false;">
         <label for="'.$id.'taskname">'.STR_TASKNAME.'&#58;</label><br />
         <input type="text" name="',$id,'taskname" id="',$id,'taskname" class="standard" size="30" value="',dirty($todo->title),'" /><br />
         <div class="ajaxerror" id="',$id,'error_taskname">&nbsp;<br /></div>
         
         <label for="',$id,'datedue">',STR_DATEDUE,'&#58;</label><br />
         <div class="datebox">
         <input type="text" name="',$id,'datedue" size="10" id="',$id,'datedue" class="standard" ';
      if($todo->due != 0) {
        echo 'value="',date("j/m/Y",$todo->due),'" ';
      }
      echo '/><img src="',$_SESSION['IZ_CALURL'],'images/calendar.png" class="datepicker" alt="',STR_DATEDUE,'" id="mousehookdatedue',$id,'" />
         </div>
         <div class="ajaxerror" id="',$id.'error_datedue">&nbsp;</div><br />';
      echo //'<br />
           '<label for="',$id,'task">',STR_TASK,'&#58;</label><br />
            <textarea rows="10" name="',$id,'task" id="',$id,'task" class="standard" cols="30">',dirty($todo->description),'</textarea><br />';
      if(($_SESSION['IZ_OPT'] & OPT_CATEGORY) != 0) {
           echo '<label for="',$id,'taskcategory">',STR_CATEGORY,'&#58;</label><br />
                 <select name="',$id,'taskcategory" id="',$id,'taskcategory" size="1" onchange="addnewcatplain(\'todo\',\'',$id,'\');">';
           get_categories('edit',$todo->category,'task');
           echo '<option id="',$id,'newcattodo" value="newcat">',STR_NEWCATEGORY,'</option>
                 </select><br />';
         }
      if($_SESSION['authed'] == 1) {
           echo '<label>',STR_TASKSTATUS,'&#58;</label><br />
    	    <div class="checks">
    	      <input type="radio" value="Public" name="',$id,'taskstatus" id="',$id,'taskstatuspublic" tabindex="17" class="nomargin"';
           if($todo->status == 'Public') {
             echo ' checked="checked"';
           }
           echo ' /><label for="',$id,'publicstatus" class="plain">',STR_STATUSPUBLIC,'</label>
    	      <input type="radio" value="Private" name="',$id,'taskstatus" id="',$id,'taskstatusprivate" tabindex="18" class="nomargin"';
           if($todo->status == 'Private') {
             echo ' checked="checked"';
           }
           echo ' /><label for="',$id,'privatestatus" class="plain">',STR_STATUSPRIVATE,'</label>
    	    </div>';
        } else {
            echo '<input type="hidden" name="',$id,'taskstatus" id="',$id,'taskstatus" value="Public" />';
        }
         echo '<br />
         <input type="button" name="cancel" value="',STR_CANCEL,'" class="nicebutton" onclick="canceledit(\'',$id,'\')" />
         <input type="submit" name="ajax" value="',STR_SAVE,'" class="nicebutton" />
         <input type="hidden" name="todo" value="edit" />
         <input type="hidden" name="id" value="',$id,'" />
         </form>';
    }  
}
function update_span($type) {
    global $curDay;
    global $curMonth;
    global $curYear;
    global $curtime;
    global $user;
    if($type == 'todo') {
      switch($_GET['todospan']) {
        case "1week":
          $_SESSION['todospan']['length'] = '1week';
          $_SESSION['todospan']['value'] = $curtime + 604800;
        break;
        case "1month":
          $_SESSION['todospan']['length'] = '1month';
          $_SESSION['todospan']['value'] = mktime(0,0,0,$curMonth+1,$curDay,$curYear);
        break;
        case "6months":
          $_SESSION['todospan']['length'] = '6months';
          $_SESSION['todospan']['value'] = mktime(0,0,0,$curMonth+6,$curDay,$curYear);
        break;
        case "1year":
          $_SESSION['todospan']['length'] = '1year';
          $_SESSION['todospan']['value'] = mktime(0,0,0,$curMonth,$curDay,$curYear+1);
        break;
        case "all":
          $_SESSION['todospan']['length'] = 'all';
          $_SESSION['todospan']['value'] = 9999999999;
        break;
      }
      $_SESSION['completedtodos'] = $_GET['includecompleted'];
      $user->saveincludecomplete();    
    }
}
?>